
package com.haierp.util.ExcelTool;

import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.haierp.model.Good;
import com.haierp.model.ItemSku;
import com.haierp.service.IGoodService;
import com.haierp.service.IItemService;
import com.haierp.service.IItemSkuService;
import com.haierp.util.DateUtil;

public class ExcelItemSkuImport {
	
	IGoodService iGoodService;
	IItemService iItemService;
	IItemSkuService iItemSkuService;	

	public ExcelItemSkuImport(
			IGoodService iGoodService,
			IItemService iItemService,
			IItemSkuService iItemSkuService) {
		this.iGoodService = iGoodService;
		this.iItemService = iItemService;
		this.iItemSkuService = iItemSkuService;
	}

	public boolean importItemSku(String name) throws Exception {

		URL url = ExcelHelper.class.getClassLoader().getResource(name);
		if (null == url) {
			
//			throw new Exception("File not found: " + name);
			return false;
		}
		
		return importItemSku(url);
	}
	
	public boolean importItemSku(URL url) throws Exception {

		if (null == url) {
			
//			throw new Exception("File not found: " + name);
			return false;
		}
		
		File file = new File(url.getPath());		
		String filepath = file.getAbsolutePath();		
		if (null == filepath) {
//			throw new Exception("File not found: " + name);
			return false;
		}

		ExcelHelper excelHelper = new ExcelHelper();
		try {
			if (excelHelper.openRead(filepath)) {
				
				ExcelSummary[] excelSummaries = excelHelper.getExcelSummaries();
				if (null == excelSummaries) {
//					System.out.println("summery is empty");
					return false;
				}
				
				Map<String, Long> allItemCodeAndIds = iItemService.queryAllItemCodeAndIdHashMap();
				
				int sheetIndex = 0;
				int startRowIndex = 1;
				do {	
					int length = Math.min(1000, excelSummaries[sheetIndex].getRowNumber() - startRowIndex);
					
					Map<Integer, Map<Integer, Object>> map = excelHelper.readSheetContent(excelSummaries[sheetIndex], startRowIndex, length);
					
					List<Good> goods = convertToGoods(map);
					iGoodService.insertBatch(goods);
					
					if (!insertItemSKU(goods, allItemCodeAndIds)) {
//						throw new Exception("Excel insert itemsku failed");
					}
					
					startRowIndex += length;
				} while (startRowIndex < excelSummaries[sheetIndex].getRowNumber());
			} else {
//				throw new Exception("Excel open file failed: " + filepath);
				return false;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			excelHelper.close();
		}
		
		return true;
	}
	
	private List<Good> convertToGoods(Map<Integer, Map<Integer, Object>> map) {
		List<Good> goods = new ArrayList<Good>();
		Good good  = new Good();
		String temp = new String();
		
		for (Map<Integer, Object> line : map.values()) {

			good = new Good();
			good.setSkuCode(line.get(0).toString());
			good.setName(line.get(1).toString());
			good.setCategoryName(line.get(2).toString());
			good.setEnName(line.get(3).toString());
			good.setItemShort(line.get(4).toString());
			good.setMainPic(line.get(5).toString());
			temp = line.get(6).toString();
			if(StringUtils.isNotEmpty(temp))
			{
				Date startDate = null;
				try {
					startDate=DateUtil.convertStr2Date(temp, DateUtil.SIMPLE_DATE_PARTEN);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				good.setStartDate(startDate);
			}
			temp = line.get(7).toString();
			if(StringUtils.isNotEmpty(temp))
			{
				Date endDate = null;
				try {
					endDate=DateUtil.convertStr2Date(temp, DateUtil.SIMPLE_DATE_PARTEN);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				good.setEndDate(endDate);
			}
			
			good.setColor(line.get(8).toString());
			good.setScale(line.get(9).toString());
			
			good.setSalePrice(Double.parseDouble(line.get(11).toString()));
			good.setAntciFreight(Double.parseDouble(line.get(12).toString()));

			Double d = Double.parseDouble(line.get(15).toString());
			good.setSalable(d.intValue());
			d = Double.parseDouble(line.get(17).toString());
			good.setSellInventory(d.intValue());
			
//			good.setSalePrice(salePrice);
			temp = line.get(14).toString();
			if(StringUtils.isNotEmpty(temp))
			{
				Date createDate = null;
				try {
					createDate=DateUtil.convertStr2Date(temp, DateUtil.DATE_PARTEN_MILLI);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				good.setGmtCreate(createDate);
			}
			
			good.setBarcode(line.get(16).toString());
			good.setBrand(line.get(18).toString());
			good.setSaleType(line.get(19).toString());
			good.setItemCode(line.get(20).toString());
//			good.setGmtCreate(new Date());
			good.setGmtModify(new Date());
			
			temp = line.get(21).toString();
			if (!"NULL".equals(temp)) {
				d = Double.parseDouble(temp);
				good.setInventory(d.intValue());	
			}
			temp = line.get(22).toString();
			if (!"NULL".equals(temp)) {
				d = Double.parseDouble(temp);
				good.setVirtualInventory(d.intValue());
			}
			goods.add(good);
		}
		
		return goods;
	}
	
	private boolean insertItemSKU(List<Good> goods, Map<String, Long> allItemCodeAndIds) {
		
		List<ItemSku> itemSkuList = new ArrayList<>();
		
		goods.forEach(good -> {
			Long id = allItemCodeAndIds.get(good.getItemCode());
			ItemSku itemSku = new ItemSku();
			itemSku.setItemId(id);
			itemSku.setItemCode(good.getItemCode());
			itemSku.setUpc(good.getBarcode());
			itemSku.setBrand(good.getBrand());
			itemSku.setCategoryName(good.getCategoryName());
			itemSku.setColor(good.getColor());
			itemSku.setGmtCreate(good.getGmtCreate());
			itemSku.setGmtModify(new Date());
//			itemSku.setInventory(good.getInventory());
//			itemSku.setMainPic(good.getMainPic());
			itemSku.setItemName(good.getName());
			itemSku.setSkuCode(good.getSkuCode());
			itemSku.setScale(good.getScale());
			itemSkuList.add(itemSku);
		});
		
		return iItemSkuService.insertBatch(itemSkuList);
	}
}
